<template>
  <div>
    <el-card class="box-card" style="margin: 20px 0">
      <CategorySelect
        :show="scene !== 0"
        @getCategoryId="getCategoryId"
      ></CategorySelect>
    </el-card>

    <el-card class="box-card">
      <div v-show="scene === 0">
        <el-button
          type="primary"
          @click="addSpu"
          icon="el-icon-plus"
          :disabled="!category3Id"
          >添加SPU</el-button
        >
        <el-table :data="records" border style="width: 100%">
          <el-table-column type="index" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                title="添加sku"
                icon="el-icon-plus"
                type="success"
                size="mini"
                @click="addSku(row)"
              ></el-button>
              <el-button
                title="修改spu"
                icon="el-icon-edit"
                type="warning"
                size="mini"
                @click="editSpu(row)"
              ></el-button>
              <el-button
                title="查看sku"
                icon="el-icon-info"
                type="info"
                size="mini"
                @click="checkSkuList(row)"
              ></el-button>
              <!-- sku列表弹窗 -->
              <el-dialog
                :title="`${spu.spuName}`"
                :visible.sync="dialogVisible"
                width="65%"
                :before-close="handleClose"
              >
                <el-table :data="skuList" v-loading="loading" border style="width: 100%">
                  <el-table-column label="名称" prop="skuName" width="width">
                  </el-table-column>
                  <el-table-column label="价格" prop="price" width="width">
                  </el-table-column>
                  <el-table-column label="重量" prop="weight" width="width">
                  </el-table-column>
                  <el-table-column label="默认图片" width="width">
                    <template slot-scope="{row,$index}">
                      <img :src="row.skuDefaultImg" style="width: 100px;height: 100px;" >
                    </template>
                  </el-table-column>
                </el-table>
              </el-dialog>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="deleteSpu(row)"
                style="margin-left: 10px"
              >
                <el-button
                  slot="reference"
                  title="删除spu"
                  icon="el-icon-delete"
                  type="danger"
                  size="mini"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <div class="block" style="text-align: center">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[3, 5, 10, 20]"
            :page-size="limit"
            layout="  prev, pager, next, jumper,-> ,sizes,total"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
      <!-- 添加SPU或修改SPU -->
      <SpuForm
        v-show="scene === 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <!-- 添加SKU -->
      <SkuForm
        v-show="scene === 2"
        @changeScene="changeScene"
        ref="sku"
      ></SkuForm>
    </el-card>
  </div>
</template>

<script>
import SpuForm from "@/views/product/Spu/SpuForm";
import SkuForm from "@/views/product/Spu/SkuForm";
import { deleteSpu } from "@/api/product/spu";
export default {
  name: "Spu",
  data() {
    return {
      //三级分类的id
      category1Id: "",
      category2Id: "",
      category3Id: "",
      //状态
      show: true,
      //spu数据
      records: null,
      //每页数据量
      limit: 3,
      //当前页
      page: 1,
      //总页数
      total: 0,
      scene: 0, //0表示展示spu数据,1表示添加或修改spu，2表示添加sku数据
      dialogVisible: false, //sku弹窗是否显示,
      spu: {},
      skuList: [], //sku弹窗数据
      loading:true,//加载效果
    };
  },
  methods: {
    //三级联动自定义回调，将子组件的数据转给父组件
    getCategoryId(categoryData) {
      if (categoryData.level === 1) {
        this.category1Id = categoryData.id;
        //清除2，3级分类id
        this.category2Id = "";
        this.category3Id = "";
      } else if (categoryData.level === 2) {
        this.category2Id = categoryData.id;
        //清除3级分类id
        this.category3Id = "";
      } else {
        this.category3Id = categoryData.id;
        this.getSpuList();
      }
    },
    //获取数据
    async getSpuList() {
      const { page, limit, category3Id } = this;
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      if (result.code === 200) {
        this.records = result.data.records;
        this.total = result.data.total;
        console.log(result);
      } else {
        this.$message.error(result.message);
      }
    },
    //分页器页数发生变化的回调
    handleCurrentChange(pages) {
      this.page = pages;
      this.getSpuList();
    },
    //每页数据量变化时的回调
    handleSizeChange(limits) {
      this.limit = limits;
      this.getSpuList();
    },
    //修改Spu
    editSpu(row) {
      this.scene = 1;
      this.$refs.spu.initspuData(row);
    },
    //改变场景
    changeScene(scene) {
      this.scene = scene;
      this.getSpuList(this.page);
    },
    //添加spu
    addSpu() {
      this.scene = 1;

      this.$refs.spu.addSpuData(this.category3Id);
    },
    //删除spu
    async deleteSpu(row) {
      let result = await this.$API.spu.deleteSpu(row.id);
      if (result.code === 200) {
        this.$message.success("删除成功");
      } else {
        this.$message.error(result.data);
      }
    },
    //添加sku
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    //查看sku数据列表
    async checkSkuList(row) {
      this.dialogVisible = true;
      this.spu = row;

      let result = await this.$API.spu.reqFindSkuList(row.id);

      if (result.code === 200) {
        this.skuList = result.data;
      }
      this.loading=false

    },
    //关闭弹窗的回调
    handleClose(done){
      this.loading=true
      this.skuList=[]
      //关闭弹窗
      done()
    }
  },
  components: {
    SkuForm,
    SpuForm,
  },
};
</script>
